﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ServiceModel.DomainServices.Client;
using MVVM_S.Samples.HelpDesk.Web.Models;

namespace MVVM_S.Samples.HelpDesk.Services
{
    /// <summary>
    /// This file should be used to return EntitySet<EntityType> for each of your 
    /// Domain Context Entity Types.  This data is used for blendability only, and
    /// is NOT used during runtime of your project.
    /// 
    /// Use the <see cref="sldesigndata">sldesigndata</see> snippet to add each property.
    /// </summary>
    public class HelpdeskDesign : EntityContainer
    {
        Random rng;
        public HelpdeskDesign()
        {
            rng = new Random();
        }


        // Place your properties Here
         
        #region Categories Property, to provide fake data for Category type
        /// <summary>
        /// Categories Property contains design-time EntitySet for Category Entities
        /// </summary>
        public EntitySet<Category> Categories
        {
            get
            {
                EntitySet _CategorySet;  // Just used for the TryGetEntitySet function

                // If this has not been setup, then setup the data
                if (TryGetEntitySet(typeof(Category), out _CategorySet) == false)
                    designCategoryData();

                // Return the EntitySet
                return GetEntitySet<Category>();
            }
        }
        /// <summary>
        /// Function to Create Design-Time Data for Category Type
        /// </summary>
        private void designCategoryData()
        {
            // Create the EntitySet
            CreateEntitySet<Category>(EntitySetOperations.All);
            
            // Now Create Design-Time Only Data
            var sampleSet = GetEntitySet<Category>();
            for (int i = 1; i <= 15; i++)
            {
                sampleSet.Attach(new Category()
                {
                    CategoryID = "CAT" + i,
                    Active = true,
                    CategoryName = "Category #"+i
                });
            }
        }
        #endregion

        
        #region Offices Property, to provide fake data for Office type
        /// <summary>
        /// Offices Property contains design-time EntitySet for Office Entities
        /// </summary>
        public EntitySet<Office> Offices
        {
            get
            {
                EntitySet _OfficeSet;  // Just used for the TryGetEntitySet function

                // If this has not been setup, then setup the data
                if (TryGetEntitySet(typeof(Office), out _OfficeSet) == false)
                    designOfficeData();

                // Return the EntitySet
                return GetEntitySet<Office>();
            }
        }
        /// <summary>
        /// Function to Create Design-Time Data for Office Type
        /// </summary>
        private void designOfficeData()
        {
            // Create the EntitySet
            CreateEntitySet<Office>(EntitySetOperations.All);

            // Now Create Design-Time Only Data
            var sampleSet = GetEntitySet<Office>();
            sampleSet.Attach(new Office()
            {
                OfficeID = "DEN",
                OfficeName = "Denver",
                Active = true
            });
            sampleSet.Attach(new Office()
            {
                OfficeID = "ATL",
                OfficeName = "Atlanta",
                Active = true
            });
            sampleSet.Attach(new Office()
            {
                OfficeID = "NYC",
                OfficeName = "New York",
                Active = true
            });
        }
        #endregion

        
        #region Users Property, to provide fake data for User type
        /// <summary>
        /// Users Property contains design-time EntitySet for User Entities
        /// </summary>
        public EntitySet<User> Users
        {
            get
            {
                EntitySet _UserSet;  // Just used for the TryGetEntitySet function

                // If this has not been setup, then setup the data
                if (TryGetEntitySet(typeof(User), out _UserSet) == false)
                    designUserData();

                // Return the EntitySet
                return GetEntitySet<User>();
            }
        }
        /// <summary>
        /// Function to Create Design-Time Data for User Type
        /// </summary>
        private void designUserData()
        {
            // Create the EntitySet
            CreateEntitySet<User>(EntitySetOperations.All);

            // Now Create Design-Time Only Data
            var sampleSet = GetEntitySet<User>();
            for (int i = 1; i <= 150; i++)
            {
                sampleSet.Attach(new User()
                {
                    UserID = i,
                    UserName = "user"+i,
                    Office = Offices.Skip(rng.Next(Offices.Count-1)).First(),
                    Phone = "111-123-0099",
                    DisplayName = "User Known as "+ rng.Next(999),
                    Active = (rng.Next(10) % 6 == 0)
                });
            }
        }
        #endregion

        
        #region Tickets Property, to provide fake data for Ticket type
        /// <summary>
        /// Tickets Property contains design-time EntitySet for Ticket Entities
        /// </summary>
        public EntitySet<Ticket> Tickets
        {
            get
            {
                EntitySet _TicketSet;  // Just used for the TryGetEntitySet function

                // If this has not been setup, then setup the data
                if (TryGetEntitySet(typeof(Ticket), out _TicketSet) == false)
                    designTicketData();

                // Return the EntitySet
                return GetEntitySet<Ticket>();
            }
        }
        /// <summary>
        /// Function to Create Design-Time Data for Ticket Type
        /// </summary>
        private void designTicketData()
        {
            // Create the EntitySet
            CreateEntitySet<Ticket>(EntitySetOperations.All);

            // Now Create Design-Time Only Data
            var sampleSet = GetEntitySet<Ticket>();
            for (int i = 1; i <= 55; i++)
            {
                sampleSet.Attach(new Ticket()
                {
                    TicketID = i,
                    Category = Categories.Skip(rng.Next(Categories.Count - 1)).First(),
                    Title = "Computer Problems",
                    Details = "I cannot start my computer.  Please help me!",
                    EscalationLevel = (short)rng.Next(3),
                    Office = Offices.Skip(rng.Next(Offices.Count-1)).First(),
                    Opened = DateTime.Now.AddHours(-rng.Next(450)),
                    TicketFor = Users.Skip(rng.Next(Users.Count-1)).First(),
                    Updated = DateTime.Now.AddHours(-rng.Next(48)),
                    UpdatedByUser = Users.Skip(rng.Next(Users.Count - 1)).First()                    
                });
            }
        }
        #endregion

        
        #region Notes Property, to provide fake data for Note type
        /// <summary>
        /// Notes Property contains design-time EntitySet for Note Entities
        /// </summary>
        public EntitySet<Note> Notes
        {
            get
            {
                EntitySet _NoteSet;  // Just used for the TryGetEntitySet function

                // If this has not been setup, then setup the data
                if (TryGetEntitySet(typeof(Note), out _NoteSet) == false)
                    designNoteData();

                // Return the EntitySet
                return GetEntitySet<Note>();
            }
        }
        /// <summary>
        /// Function to Create Design-Time Data for Note Type
        /// </summary>
        private void designNoteData()
        {
            // Create the EntitySet
            CreateEntitySet<Note>(EntitySetOperations.All);

            // Now Create Design-Time Only Data
            var sampleSet = GetEntitySet<Note>();
            int i = 1;
            foreach(Ticket ticket in Tickets)
            {
                sampleSet.Attach(new Note()
                {
                    NotesID = i,
                    Entered = ticket.Updated.Value,
                    Text = "This are my notes about this issue.  Please update this ticket as needed.",
                    Ticket = ticket,
                    EnteredBy = Users.Skip(rng.Next(Users.Count -1)).First().DisplayName
                });
                i++;
            }
        }
        #endregion
                
    }
}